/*
 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebView.h"
#include "wtf/text/WTFString.h"
#include "wtf/ThreadIdentifierDataPthreads.h"
#include "wtf/MainThread.h"
#include "wtf/Threading.h"
#include "runtime/InitializeThreading.h"
#include <cdroid.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <time.h>
using namespace cdroid;

static void ms2timespec(int ms, struct timespec *ts){
    ts->tv_sec = ms / 1000;
    ts->tv_nsec = (ms % 1000) * 1000000;
}
static int fdcallback(int fd, int events, void* data){
   uint64_t count;
   static int loops=0;
   struct timespec cur;
   clock_gettime(CLOCK_MONOTONIC,&cur);
   if(events&Looper::EVENT_INPUT)
      ::read(fd, &count, sizeof(uint64_t));
   printf("..fd=%d evnets=%d [%4d] time=%lld.%lld\r\n",fd,events,loops++,cur.tv_sec,cur.tv_nsec/1000000);
   if(loops>10){
      struct itimerspec new_value={{0,0},{0,0}};
      timerfd_settime(fd,0,&new_value, NULL);
      Looper::getDefault()->removeFd(fd);
   }
   return 1;
}

int main(int argc,const char*argv[])
{
    App app(argc,argv);
    unsigned int width,height;
    GFXGetDisplaySize(0,&width,&height);
    if((GFXGetRotation(0)==ROTATE_90)||(GFXGetRotation(0)==270)){
	int tmp = width;
	width = height;
	height=tmp;
    }
    Window *w=new Window(0,0,width,height);
    WebKit::WebView*webView=new WebKit::WebView(width,height);
    std::string url = app.getParam(0,"https://www.sina.com.cn");
    printf("windowsize=%dx%d url=%s\r\n",width,height,url.c_str());
    webView->loadUrl(WTF::String(url.c_str())); 
    w->addView(webView);

    Looper*loop= Looper::getDefault();
    struct itimerspec new_value={{0,0},{0,0}};
    ms2timespec(200,&new_value.it_value);

    ms2timespec(200,&new_value.it_interval);
    int fd=timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);

    int rc=timerfd_settime(fd, 0/*TFD_TIMER_ABSTIME*/, &new_value, NULL);
    printf("looper=%p..fd= %d cbk=%p rc=%d\r\n",loop,fd,fdcallback,rc);
    //loop->addFd(fd,0,Looper::EVENT_INPUT,fdcallback,nullptr);
    app.exec();
    return 0;
}
